﻿using System;
using System.Data;
using System.Collections.Generic;
using CrazyTalk.Caching;
using CrazyTalk.DALFactory;
using CrazyTalk.IDAL;
using CrazyTalk.Models;
using CrazyTalk.Common;
using DataCache = CrazyTalk.DALFactory.DataCache;

namespace CrazyTalk.BLL
{
    /// <summary>
    /// MenuBLL
    /// </summary>
    public class MenuBLL
    {
        private static MenuBLL _instance;
        private static readonly object SyncLock = new object();
        public static MenuBLL Instance
        {
            get
            {
                lock (SyncLock)
                {
                    if (_instance == null)
                    {
                        _instance = new MenuBLL();
                    }
                }
                return _instance;
            }
        }
        private readonly IMenuDAL dal = DataAccess.Get<IMenuDAL>();
        public MenuBLL()
        { }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string MenuID)
        {
            return dal.Exists(MenuID);
        }
        //获取最大ID
        public int GetMaxId(List<string> fields, List<object> values)
        {
            List<int> list = dal.GetAllId(fields, values);
            int max = 0;
            for (int i = 0; i < list.Count; i++)
            {
                int temp = list[i];
                if (max < temp)
                {
                    max = temp;
                }
            }
            return max;
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(List<string> fields, List<object> values)
        {
            return dal.Exists(fields, values);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Menu model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Menu model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(List<string> fields, List<object> values)
        {
            return dal.Update(fields, values);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string MenuID)
        {

            return dal.Delete(MenuID);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string MenuIDlist)
        {
            return dal.DeleteList(MenuIDlist);
        }
        /// <summary>
        /// 从缓存获取用户的显示菜单列表
        /// </summary>
        public IList<MenuNew> GetMenuListByCache(string userId, int roleId)
        {
            DefaultCache cache = DefaultCache.GetCacheService();
            string cacheKey = CacheKeys.Menu_List_User + userId;
            IList<MenuNew> list = cache.RetrieveObject(cacheKey) as IList<MenuNew>;
            if (list == null)
            {
                DataTable dtb = dal.GetMenuList(userId, roleId);
                list = ModelConvertHelper<MenuNew>.ConvertToModel(dtb);
                if (list != null)
                {
                    cache.AddObject(cacheKey, list);
                }
            }
            return list;
        }

        /// <summary>
        /// 根据输入的用户名和角色编号，获取用户的显示菜单列表
        /// </summary>
        /// <param name="userId">用户名</param>
        /// <param name="roleId">角色编号</param>
        /// <returns></returns>
        public DataTable GetMenuList(string userId, int roleId)
        {
            return dal.GetMenuList(userId, roleId);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Menu GetModel(string MenuID)
        {

            return dal.GetModel(MenuID);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public Menu GetModelByCache(string MenuID)
        {

            string CacheKey = "MenuModel-" + MenuID;
            object objModel = DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(MenuID);
                    if (objModel != null)
                    {
                        int ModelCache = ConfigHelper.GetConfigInt("ModelCache");
                        DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (Menu)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(List<string> fields, List<object> values)
        {
            return dal.GetList(fields, values);
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetListUnionPermissionList(List<string> fields, List<object> values)
        {
            return dal.GetListUnionPermissionList(fields, values);
        }


        /// <summary>
        /// 获取隐藏菜单的权限控制列表
        /// </summary>
        /// <param name="roleId">限定的角色编号</param>
        /// <returns></returns>
        public DataTable GetHiddenMenuList(int roleId)
        {
            return dal.GetHiddenMenuList(roleId);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetListUnionMenu(List<string> fields, List<object> values)
        {
            return dal.GetListUnionMenu(fields, values);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, List<string> fields, List<object> values, string filedOrder)
        {
            return dal.GetList(Top, fields, values, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Menu> GetModelList(List<string> fields, List<object> values)
        {
            DataSet ds = dal.GetList(fields, values);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 绕过报表权限控制，提取所有报表页面
        /// </summary>
        /// <param name="list">需要获取的报表的MenuID</param>
        /// <returns></returns>
        public List<Menu> GetReportsList(List<string> list)
        {
            if (list.Count <= 0)
            {
                return null;
            }
            DataSet ds = dal.GetReportsList(list);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Menu> GetModelListPermission(List<string> fields, List<object> values)
        {
            DataSet ds = dal.GetListPermission(fields, values);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表(拥有网吧连锁公司父级的获取菜单方式)
        /// </summary>
        public List<Menu> GetModelListPermission2(List<string> fields, List<object> values, string parentId)
        {
            DataSet ds = dal.GetListPermission2(fields, values, parentId);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Menu> DataTableToList(DataTable dt)
        {
            List<Menu> modelList = new List<Menu>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Menu model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Menu();
                    if (dt.Rows[n]["MenuID"] != null && dt.Rows[n]["MenuID"].ToString() != "")
                    {
                        model.MenuID = dt.Rows[n]["MenuID"].ToString();
                    }
                    if (dt.Rows[n]["MenuUrl"] != null && dt.Rows[n]["MenuUrl"].ToString() != "")
                    {
                        model.MenuUrl = dt.Rows[n]["MenuUrl"].ToString();
                    }
                    if (dt.Rows[n]["MenuName"] != null && dt.Rows[n]["MenuName"].ToString() != "")
                    {
                        model.MenuName = dt.Rows[n]["MenuName"].ToString();
                    }
                    if (dt.Rows[n]["MenuLevel"] != null && dt.Rows[n]["MenuLevel"].ToString() != "")
                    {
                        model.MenuLevel = dt.Rows[n]["MenuLevel"].ToString();
                    }
                    if (dt.Rows[n]["ParentMenu"] != null && dt.Rows[n]["ParentMenu"].ToString() != "")
                    {
                        model.ParentMenu = dt.Rows[n]["ParentMenu"].ToString();
                    }
                    if (dt.Rows[n]["SortId"] != null && dt.Rows[n]["SortId"].ToString() != "")
                    {
                        model.SortId = int.Parse(dt.Rows[n]["SortId"].ToString());
                    }
                    if (dt.Rows[n]["Status"] != null && dt.Rows[n]["Status"].ToString() != "")
                    {
                        model.Status = int.Parse(dt.Rows[n]["Status"].ToString());
                    }
                    if (dt.Rows[n]["PicName"] != null && dt.Rows[n]["PicName"].ToString() != "")
                    {
                        model.PicName = dt.Rows[n]["PicName"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList(null, null);
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(List<string> fields, List<object> values)
        {
            return dal.GetRecordCount(fields, values);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(List<string> fields, List<object> values, string orderby, int startIndex, int endIndex)
        {
            return dal.GetListByPage(fields, values, orderby, startIndex, endIndex);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,List<string> fields, List<object> values)
        //{
        //return dal.GetList(PageSize,PageIndex,fields, values);
        //}

    }
}

